﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.InsertLocationInsideSeam.CApIlisCopyUtils
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System.Collections.ObjectModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.InsertLocationInsideSeam;

public class CApIlisCopyUtils
{
  public static void CopyParametersAndOLP(
    ITxRoboticLocationOperation source,
    ITxRoboticLocationOperation target)
  {
    CApIlisCopyUtils.CopyParameters(source, target);
    CApIlisCopyUtils.CopyOLPCommands(source, target);
  }

  public static void CopyParametersOnly(
    ITxRoboticLocationOperation source,
    ITxRoboticLocationOperation target)
  {
    CApIlisCopyUtils.CopyParameters(source, target);
  }

  private static void CopyParameters(
    ITxRoboticLocationOperation source,
    ITxRoboticLocationOperation target)
  {
    if (((ITxRoboticOperation) target).Parameters != null)
    {
      foreach (TxRoboticParam parameter in ((ITxRoboticOperation) target).Parameters)
      {
        if (source != null)
          ((ITxRoboticOperation) target).RemoveParameter(parameter.Type);
      }
    }
    foreach (TxRoboticParam parameter in ((ITxRoboticOperation) source).Parameters)
      ((ITxRoboticOperation) target).SetParameter(parameter);
    ((ITxRoboticOperation) target).RemoveParameter("POSITION_IN_SIMULATION");
  }

  private static void DeleteCommands(ITxRoboticLocationOperation target)
  {
    if (((ITxRoboticOperation) target).Commands == null)
      return;
    foreach (ITxObject command in (Collection<ITxObject>) ((ITxRoboticOperation) target).Commands)
      command.Delete();
  }

  private static void CreateRoboticCompositeCommand(
    TxRoboticCompositeCommand command,
    ITxRoboticLocationOperation target)
  {
    TxRoboticCompositeCommandCreationData commandCreationData = new TxRoboticCompositeCommandCreationData(command.Elements);
    ((TxObjectCreationData) commandCreationData).Name = ((TxRoboticCommand) command).Name;
    TxRoboticCompositeCommand compositeCommand = ((ITxRoboticOperation) target).CreateCompositeCommand(commandCreationData);
    if (((TxRoboticCommand) command).Attributes != null)
    {
      foreach (TxAttribute attribute in ((TxRoboticCommand) command).Attributes)
        ((TxRoboticCommand) compositeCommand).SetAttribute(attribute);
    }
    ((TxRoboticCommand) compositeCommand).Text = ((TxRoboticCommand) command).Text;
  }

  private static void CreateRoboticCommand(
    TxRoboticCommand command,
    ITxRoboticLocationOperation target)
  {
    if (command.Attributes == null || target == null)
      return;
    TxRoboticCommandCreationData commandCreationData = new TxRoboticCommandCreationData(command.Text);
    ((TxObjectCreationData) commandCreationData).Name = command.Name;
    TxRoboticCommand command1 = ((ITxRoboticOperation) target).CreateCommand(commandCreationData);
    foreach (TxAttribute attribute in command.Attributes)
      command1.SetAttribute(attribute);
  }

  private static void CopyNewCommands(
    ITxRoboticLocationOperation source,
    ITxRoboticLocationOperation target)
  {
    if (((ITxRoboticOperation) source).Commands == null || ((ITxRoboticOperation) target).Commands == null)
      return;
    foreach (ITxObject command in (Collection<ITxObject>) ((ITxRoboticOperation) source).Commands)
    {
      if (command is TxRoboticCompositeCommand)
        CApIlisCopyUtils.CreateRoboticCompositeCommand(command as TxRoboticCompositeCommand, target);
      else if (command is TxRoboticCommand)
        CApIlisCopyUtils.CreateRoboticCommand(command as TxRoboticCommand, target);
    }
  }

  private static void CopyOLPCommands(
    ITxRoboticLocationOperation source,
    ITxRoboticLocationOperation target)
  {
    if (source == null || target == null)
      return;
    CApIlisCopyUtils.DeleteCommands(target);
    CApIlisCopyUtils.CopyNewCommands(source, target);
  }

  public enum CopyParametersOptions
  {
    None,
    Robotic,
    RoboticAndOLP,
  }
}
